package com.example.servicetest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MyService.DownloadBinder downloadBinder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadBinder = (MyService.DownloadBinder) service;
            downloadBinder.startDownload();
            downloadBinder.getProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button startService = (Button) findViewById(R.id.start_service);
        Button stopService = (Button) findViewById(R.id.stop_service);
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);

        Button bindService = (Button) findViewById(R.id.bind_service);
        Button unbindService = (Button) findViewById(R.id.unbind_service);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);

        Log.d("MyService","OnCreate executed");

        Intent intent = new Intent(this,MianActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this,0,item,0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("This is content title")
                .setContentText("This is content text")
                .setWhen(System.currentTimeMillis())
                .setLargeIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pi)
                .build();
        startForeground(1,notification);

        Button startIntentService = (Button) findViewById(R.id.start_intent_service);
        startIntentService.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_service:
                Intent startIntent = new Intent(this,MyService.class);
                startService(startIntent);  // 启动服务
                break;

            case R.id.stop_service:
                Intent stopIntent = new Intent(this,MyService.class);
                startService(stopIntent);   // 停止服务
                break;

            case R.id.bind_service:
                Intent bindIntent = new Intent(this,MyService.class);
                startService(bindIntent,connection,BIND_AUTO_CREATE);   // 绑定服务
                break;

            case R.id.start_intent_service:
                // 打印主线程 id
                Log.d("MainActivity","Thread id is" + Thread.currentThread().getId());
                Intent intentService = new Intent(this,MyIntentService.class);
                startService(intentService);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRequestPermissionResult(int requestCode,String[] permissions,int[] grantResult){
        switch (requestCode){
            case 1:
                if(grantResult.length >0 && grantResult[0] != PackageManager.PERMISSION_GRANTED);{
                Toast.makeText(this,"拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
                finish();
            }
            break;
        default:
        }
    }

    @Override
    protected void onDestory(){
        super.onDestroy();
    }
}